<template>
  <div class="echart" id="mychart1" style="width: 100%; height: 100%"></div>
</template>

<script>
import * as echarts from "echarts";
import { BlsSheBeiBaoJingTongJi } from "../../https/api/user";
export default {
  data() {
    return {
      list: [],
      time: null

    };
  },
  mounted() {
    this.initEcharts();
    this.ZongHeYuJing();
    this.time = setInterval(() => {
      this.initEcharts();
      this.ZongHeYuJing();

    }, 1000);
  },
  methods: {
    initEcharts() {
      // 基本柱状图
      const option = {
        xAxis: {
          data: this.list.map(i => i.Name),
          axisPointer: {
            type: "shadow",
          },
          axisLine: {
            lineStyle: {
              // 设置x轴颜色
              color: "rgb(242,148,85)",
            },
          },
          axisLabel: {
            rotate: -25, //代表逆时针旋转
          },
        },
        dataZoom: [
          {
            type: "inside", //无滑动条内置缩放   type: 'slider', //缩放滑动条
            show: true, //开启
            xAxisIndex: [0], //Y轴滑动
            //  xAxisIndex: [0],//X轴滑动
            start: 0, //初始化时，滑动条宽度开始标度
            end: 100, //初始化时，滑动条宽度结束标度
          },
        ],
        tooltip: {
          trigger: 'axis',
          formatter: this.customTooltip
        },
        grid: {
          top: 35,
          left: 30,
          right: 30,
          bottom: 35,
        },
        yAxis: {
          name: "报警(次)",
          axisLine: {
            lineStyle: {
              // 设置y轴颜色
              color: "#87CEFA",
            },
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "#87CEFA",
              type: "dashed",
            },
          },
        },
        series: [
          {
            type: "bar", //形状为柱状图
            data: this.list.map(i => i.Count),
            // barWidth: 15, //柱子宽度

            label: {
              show: true, // 显示标签
              textStyle: {
                fontSize: 12, // 设置文字大小为16px
              },
            },
          },
        ],
      };
      const myChart = echarts.init(document.getElementById("mychart1"));
      myChart.setOption(option);
      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    ZongHeYuJing() {
      const data = {
        y: 1
      }
      BlsSheBeiBaoJingTongJi(data).then((res) => {
        console.log("巴拉素柱状图", res);
        if (res) {
          this.list = res
        }
        this.initEcharts();
      });

    },
    customTooltip(params) {
      return `${params[0].name}：${params[0].value}次报警`;
    }
  },
};
</script>
